<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>textrange.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_dom_textrange.js.html">textrange.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities for working with text ranges in HTML documents.
<a name="line17"></a> *
<a name="line18"></a> * @author robbyw@google.com (Robby Walker)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.dom.TextRange&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.array&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom.AbstractRange&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.RangeType&#39;);
<a name="line28"></a>goog.require(&#39;goog.dom.SavedRange&#39;);
<a name="line29"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line30"></a>goog.require(&#39;goog.dom.TextRangeIterator&#39;);
<a name="line31"></a>goog.require(&#39;goog.dom.browserrange&#39;);
<a name="line32"></a>goog.require(&#39;goog.string&#39;);
<a name="line33"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * Create a new text selection with no properties.  Do not use this constructor:
<a name="line39"></a> * use one of the goog.dom.Range.createFrom* methods instead.
<a name="line40"></a> * @constructor
<a name="line41"></a> * @extends {goog.dom.AbstractRange}
<a name="line42"></a> * @final
<a name="line43"></a> */
<a name="line44"></a>goog.dom.TextRange = function() {
<a name="line45"></a>};
<a name="line46"></a>goog.inherits(goog.dom.TextRange, goog.dom.AbstractRange);
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * Create a new range wrapper from the given browser range object.  Do not use
<a name="line51"></a> * this method directly - please use goog.dom.Range.createFrom* instead.
<a name="line52"></a> * @param {Range|TextRange} range The browser range object.
<a name="line53"></a> * @param {boolean=} opt_isReversed Whether the focus node is before the anchor
<a name="line54"></a> *     node.
<a name="line55"></a> * @return {goog.dom.TextRange} A range wrapper object.
<a name="line56"></a> */
<a name="line57"></a>goog.dom.TextRange.createFromBrowserRange = function(range, opt_isReversed) {
<a name="line58"></a>  return goog.dom.TextRange.createFromBrowserRangeWrapper_(
<a name="line59"></a>      goog.dom.browserrange.createRange(range), opt_isReversed);
<a name="line60"></a>};
<a name="line61"></a>
<a name="line62"></a>
<a name="line63"></a>/**
<a name="line64"></a> * Create a new range wrapper from the given browser range wrapper.
<a name="line65"></a> * @param {goog.dom.browserrange.AbstractRange} browserRange The browser range
<a name="line66"></a> *     wrapper.
<a name="line67"></a> * @param {boolean=} opt_isReversed Whether the focus node is before the anchor
<a name="line68"></a> *     node.
<a name="line69"></a> * @return {!goog.dom.TextRange} A range wrapper object.
<a name="line70"></a> * @private
<a name="line71"></a> */
<a name="line72"></a>goog.dom.TextRange.createFromBrowserRangeWrapper_ = function(browserRange,
<a name="line73"></a>    opt_isReversed) {
<a name="line74"></a>  var range = new goog.dom.TextRange();
<a name="line75"></a>
<a name="line76"></a>  // Initialize the range as a browser range wrapper type range.
<a name="line77"></a>  range.browserRangeWrapper_ = browserRange;
<a name="line78"></a>  range.isReversed_ = !!opt_isReversed;
<a name="line79"></a>
<a name="line80"></a>  return range;
<a name="line81"></a>};
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Create a new range wrapper that selects the given node&#39;s text.  Do not use
<a name="line86"></a> * this method directly - please use goog.dom.Range.createFrom* instead.
<a name="line87"></a> * @param {Node} node The node to select.
<a name="line88"></a> * @param {boolean=} opt_isReversed Whether the focus node is before the anchor
<a name="line89"></a> *     node.
<a name="line90"></a> * @return {goog.dom.TextRange} A range wrapper object.
<a name="line91"></a> */
<a name="line92"></a>goog.dom.TextRange.createFromNodeContents = function(node, opt_isReversed) {
<a name="line93"></a>  return goog.dom.TextRange.createFromBrowserRangeWrapper_(
<a name="line94"></a>      goog.dom.browserrange.createRangeFromNodeContents(node),
<a name="line95"></a>      opt_isReversed);
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Create a new range wrapper that selects the area between the given nodes,
<a name="line101"></a> * accounting for the given offsets.  Do not use this method directly - please
<a name="line102"></a> * use goog.dom.Range.createFrom* instead.
<a name="line103"></a> * @param {Node} anchorNode The node to start with.
<a name="line104"></a> * @param {number} anchorOffset The offset within the node to start.
<a name="line105"></a> * @param {Node} focusNode The node to end with.
<a name="line106"></a> * @param {number} focusOffset The offset within the node to end.
<a name="line107"></a> * @return {!goog.dom.TextRange} A range wrapper object.
<a name="line108"></a> */
<a name="line109"></a>goog.dom.TextRange.createFromNodes = function(anchorNode, anchorOffset,
<a name="line110"></a>    focusNode, focusOffset) {
<a name="line111"></a>  var range = new goog.dom.TextRange();
<a name="line112"></a>  range.isReversed_ = goog.dom.Range.isReversed(anchorNode, anchorOffset,
<a name="line113"></a>      focusNode, focusOffset);
<a name="line114"></a>
<a name="line115"></a>  // Avoid selecting terminal elements directly
<a name="line116"></a>  if (goog.dom.isElement(anchorNode) &amp;&amp; !goog.dom.canHaveChildren(anchorNode)) {
<a name="line117"></a>    var parent = anchorNode.parentNode;
<a name="line118"></a>    anchorOffset = goog.array.indexOf(parent.childNodes, anchorNode);
<a name="line119"></a>    anchorNode = parent;
<a name="line120"></a>  }
<a name="line121"></a>
<a name="line122"></a>  if (goog.dom.isElement(focusNode) &amp;&amp; !goog.dom.canHaveChildren(focusNode)) {
<a name="line123"></a>    var parent = focusNode.parentNode;
<a name="line124"></a>    focusOffset = goog.array.indexOf(parent.childNodes, focusNode);
<a name="line125"></a>    focusNode = parent;
<a name="line126"></a>  }
<a name="line127"></a>
<a name="line128"></a>  // Initialize the range as a W3C style range.
<a name="line129"></a>  if (range.isReversed_) {
<a name="line130"></a>    range.startNode_ = focusNode;
<a name="line131"></a>    range.startOffset_ = focusOffset;
<a name="line132"></a>    range.endNode_ = anchorNode;
<a name="line133"></a>    range.endOffset_ = anchorOffset;
<a name="line134"></a>  } else {
<a name="line135"></a>    range.startNode_ = anchorNode;
<a name="line136"></a>    range.startOffset_ = anchorOffset;
<a name="line137"></a>    range.endNode_ = focusNode;
<a name="line138"></a>    range.endOffset_ = focusOffset;
<a name="line139"></a>  }
<a name="line140"></a>
<a name="line141"></a>  return range;
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>// Representation 1: a browser range wrapper.
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>/**
<a name="line149"></a> * The browser specific range wrapper.  This can be null if one of the other
<a name="line150"></a> * representations of the range is specified.
<a name="line151"></a> * @type {goog.dom.browserrange.AbstractRange?}
<a name="line152"></a> * @private
<a name="line153"></a> */
<a name="line154"></a>goog.dom.TextRange.prototype.browserRangeWrapper_ = null;
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>// Representation 2: two endpoints specified as nodes + offsets
<a name="line158"></a>
<a name="line159"></a>
<a name="line160"></a>/**
<a name="line161"></a> * The start node of the range.  This can be null if one of the other
<a name="line162"></a> * representations of the range is specified.
<a name="line163"></a> * @type {Node}
<a name="line164"></a> * @private
<a name="line165"></a> */
<a name="line166"></a>goog.dom.TextRange.prototype.startNode_ = null;
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * The start offset of the range.  This can be null if one of the other
<a name="line171"></a> * representations of the range is specified.
<a name="line172"></a> * @type {?number}
<a name="line173"></a> * @private
<a name="line174"></a> */
<a name="line175"></a>goog.dom.TextRange.prototype.startOffset_ = null;
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * The end node of the range.  This can be null if one of the other
<a name="line180"></a> * representations of the range is specified.
<a name="line181"></a> * @type {Node}
<a name="line182"></a> * @private
<a name="line183"></a> */
<a name="line184"></a>goog.dom.TextRange.prototype.endNode_ = null;
<a name="line185"></a>
<a name="line186"></a>
<a name="line187"></a>/**
<a name="line188"></a> * The end offset of the range.  This can be null if one of the other
<a name="line189"></a> * representations of the range is specified.
<a name="line190"></a> * @type {?number}
<a name="line191"></a> * @private
<a name="line192"></a> */
<a name="line193"></a>goog.dom.TextRange.prototype.endOffset_ = null;
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Whether the focus node is before the anchor node.
<a name="line198"></a> * @type {boolean}
<a name="line199"></a> * @private
<a name="line200"></a> */
<a name="line201"></a>goog.dom.TextRange.prototype.isReversed_ = false;
<a name="line202"></a>
<a name="line203"></a>
<a name="line204"></a>// Method implementations
<a name="line205"></a>
<a name="line206"></a>
<a name="line207"></a>/**
<a name="line208"></a> * @return {!goog.dom.TextRange} A clone of this range.
<a name="line209"></a> * @override
<a name="line210"></a> */
<a name="line211"></a>goog.dom.TextRange.prototype.clone = function() {
<a name="line212"></a>  var range = new goog.dom.TextRange();
<a name="line213"></a>  range.browserRangeWrapper_ = this.browserRangeWrapper_;
<a name="line214"></a>  range.startNode_ = this.startNode_;
<a name="line215"></a>  range.startOffset_ = this.startOffset_;
<a name="line216"></a>  range.endNode_ = this.endNode_;
<a name="line217"></a>  range.endOffset_ = this.endOffset_;
<a name="line218"></a>  range.isReversed_ = this.isReversed_;
<a name="line219"></a>
<a name="line220"></a>  return range;
<a name="line221"></a>};
<a name="line222"></a>
<a name="line223"></a>
<a name="line224"></a>/** @override */
<a name="line225"></a>goog.dom.TextRange.prototype.getType = function() {
<a name="line226"></a>  return goog.dom.RangeType.TEXT;
<a name="line227"></a>};
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>/** @override */
<a name="line231"></a>goog.dom.TextRange.prototype.getBrowserRangeObject = function() {
<a name="line232"></a>  return this.getBrowserRangeWrapper_().getBrowserRange();
<a name="line233"></a>};
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>/** @override */
<a name="line237"></a>goog.dom.TextRange.prototype.setBrowserRangeObject = function(nativeRange) {
<a name="line238"></a>  // Test if it&#39;s a control range by seeing if a control range only method
<a name="line239"></a>  // exists.
<a name="line240"></a>  if (goog.dom.AbstractRange.isNativeControlRange(nativeRange)) {
<a name="line241"></a>    return false;
<a name="line242"></a>  }
<a name="line243"></a>  this.browserRangeWrapper_ = goog.dom.browserrange.createRange(
<a name="line244"></a>      nativeRange);
<a name="line245"></a>  this.clearCachedValues_();
<a name="line246"></a>  return true;
<a name="line247"></a>};
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>/**
<a name="line251"></a> * Clear all cached values.
<a name="line252"></a> * @private
<a name="line253"></a> */
<a name="line254"></a>goog.dom.TextRange.prototype.clearCachedValues_ = function() {
<a name="line255"></a>  this.startNode_ = this.startOffset_ = this.endNode_ = this.endOffset_ = null;
<a name="line256"></a>};
<a name="line257"></a>
<a name="line258"></a>
<a name="line259"></a>/** @override */
<a name="line260"></a>goog.dom.TextRange.prototype.getTextRangeCount = function() {
<a name="line261"></a>  return 1;
<a name="line262"></a>};
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/** @override */
<a name="line266"></a>goog.dom.TextRange.prototype.getTextRange = function(i) {
<a name="line267"></a>  return this;
<a name="line268"></a>};
<a name="line269"></a>
<a name="line270"></a>
<a name="line271"></a>/**
<a name="line272"></a> * @return {goog.dom.browserrange.AbstractRange} The range wrapper object.
<a name="line273"></a> * @private
<a name="line274"></a> */
<a name="line275"></a>goog.dom.TextRange.prototype.getBrowserRangeWrapper_ = function() {
<a name="line276"></a>  return this.browserRangeWrapper_ ||
<a name="line277"></a>      (this.browserRangeWrapper_ = goog.dom.browserrange.createRangeFromNodes(
<a name="line278"></a>          this.getStartNode(), this.getStartOffset(),
<a name="line279"></a>          this.getEndNode(), this.getEndOffset()));
<a name="line280"></a>};
<a name="line281"></a>
<a name="line282"></a>
<a name="line283"></a>/** @override */
<a name="line284"></a>goog.dom.TextRange.prototype.getContainer = function() {
<a name="line285"></a>  return this.getBrowserRangeWrapper_().getContainer();
<a name="line286"></a>};
<a name="line287"></a>
<a name="line288"></a>
<a name="line289"></a>/** @override */
<a name="line290"></a>goog.dom.TextRange.prototype.getStartNode = function() {
<a name="line291"></a>  return this.startNode_ ||
<a name="line292"></a>      (this.startNode_ = this.getBrowserRangeWrapper_().getStartNode());
<a name="line293"></a>};
<a name="line294"></a>
<a name="line295"></a>
<a name="line296"></a>/** @override */
<a name="line297"></a>goog.dom.TextRange.prototype.getStartOffset = function() {
<a name="line298"></a>  return this.startOffset_ != null ? this.startOffset_ :
<a name="line299"></a>      (this.startOffset_ = this.getBrowserRangeWrapper_().getStartOffset());
<a name="line300"></a>};
<a name="line301"></a>
<a name="line302"></a>
<a name="line303"></a>/** @override */
<a name="line304"></a>goog.dom.TextRange.prototype.getStartPosition = function() {
<a name="line305"></a>  return this.isReversed() ?
<a name="line306"></a>      this.getBrowserRangeWrapper_().getEndPosition() :
<a name="line307"></a>      this.getBrowserRangeWrapper_().getStartPosition();
<a name="line308"></a>};
<a name="line309"></a>
<a name="line310"></a>
<a name="line311"></a>/** @override */
<a name="line312"></a>goog.dom.TextRange.prototype.getEndNode = function() {
<a name="line313"></a>  return this.endNode_ ||
<a name="line314"></a>      (this.endNode_ = this.getBrowserRangeWrapper_().getEndNode());
<a name="line315"></a>};
<a name="line316"></a>
<a name="line317"></a>
<a name="line318"></a>/** @override */
<a name="line319"></a>goog.dom.TextRange.prototype.getEndOffset = function() {
<a name="line320"></a>  return this.endOffset_ != null ? this.endOffset_ :
<a name="line321"></a>      (this.endOffset_ = this.getBrowserRangeWrapper_().getEndOffset());
<a name="line322"></a>};
<a name="line323"></a>
<a name="line324"></a>
<a name="line325"></a>/** @override */
<a name="line326"></a>goog.dom.TextRange.prototype.getEndPosition = function() {
<a name="line327"></a>  return this.isReversed() ?
<a name="line328"></a>      this.getBrowserRangeWrapper_().getStartPosition() :
<a name="line329"></a>      this.getBrowserRangeWrapper_().getEndPosition();
<a name="line330"></a>};
<a name="line331"></a>
<a name="line332"></a>
<a name="line333"></a>/**
<a name="line334"></a> * Moves a TextRange to the provided nodes and offsets.
<a name="line335"></a> * @param {Node} startNode The node to start with.
<a name="line336"></a> * @param {number} startOffset The offset within the node to start.
<a name="line337"></a> * @param {Node} endNode The node to end with.
<a name="line338"></a> * @param {number} endOffset The offset within the node to end.
<a name="line339"></a> * @param {boolean} isReversed Whether the range is reversed.
<a name="line340"></a> */
<a name="line341"></a>goog.dom.TextRange.prototype.moveToNodes = function(startNode, startOffset,
<a name="line342"></a>                                                    endNode, endOffset,
<a name="line343"></a>                                                    isReversed) {
<a name="line344"></a>  this.startNode_ = startNode;
<a name="line345"></a>  this.startOffset_ = startOffset;
<a name="line346"></a>  this.endNode_ = endNode;
<a name="line347"></a>  this.endOffset_ = endOffset;
<a name="line348"></a>  this.isReversed_ = isReversed;
<a name="line349"></a>  this.browserRangeWrapper_ = null;
<a name="line350"></a>};
<a name="line351"></a>
<a name="line352"></a>
<a name="line353"></a>/** @override */
<a name="line354"></a>goog.dom.TextRange.prototype.isReversed = function() {
<a name="line355"></a>  return this.isReversed_;
<a name="line356"></a>};
<a name="line357"></a>
<a name="line358"></a>
<a name="line359"></a>/** @override */
<a name="line360"></a>goog.dom.TextRange.prototype.containsRange = function(otherRange,
<a name="line361"></a>                                                      opt_allowPartial) {
<a name="line362"></a>  var otherRangeType = otherRange.getType();
<a name="line363"></a>  if (otherRangeType == goog.dom.RangeType.TEXT) {
<a name="line364"></a>    return this.getBrowserRangeWrapper_().containsRange(
<a name="line365"></a>        otherRange.getBrowserRangeWrapper_(), opt_allowPartial);
<a name="line366"></a>  } else if (otherRangeType == goog.dom.RangeType.CONTROL) {
<a name="line367"></a>    var elements = otherRange.getElements();
<a name="line368"></a>    var fn = opt_allowPartial ? goog.array.some : goog.array.every;
<a name="line369"></a>    return fn(elements, function(el) {
<a name="line370"></a>      return this.containsNode(el, opt_allowPartial);
<a name="line371"></a>    }, this);
<a name="line372"></a>  }
<a name="line373"></a>  return false;
<a name="line374"></a>};
<a name="line375"></a>
<a name="line376"></a>
<a name="line377"></a>/**
<a name="line378"></a> * Tests if the given node is in a document.
<a name="line379"></a> * @param {Node} node The node to check.
<a name="line380"></a> * @return {boolean} Whether the given node is in the given document.
<a name="line381"></a> */
<a name="line382"></a>goog.dom.TextRange.isAttachedNode = function(node) {
<a name="line383"></a>  if (goog.userAgent.IE &amp;&amp; !goog.userAgent.isDocumentModeOrHigher(9)) {
<a name="line384"></a>    var returnValue = false;
<a name="line385"></a>    /** @preserveTry */
<a name="line386"></a>    try {
<a name="line387"></a>      returnValue = node.parentNode;
<a name="line388"></a>    } catch (e) {
<a name="line389"></a>      // IE sometimes throws Invalid Argument errors when a node is detached.
<a name="line390"></a>      // Note: trying to return a value from the above try block can cause IE
<a name="line391"></a>      // to crash.  It is necessary to use the local returnValue
<a name="line392"></a>    }
<a name="line393"></a>    return !!returnValue;
<a name="line394"></a>  } else {
<a name="line395"></a>    return goog.dom.contains(node.ownerDocument.body, node);
<a name="line396"></a>  }
<a name="line397"></a>};
<a name="line398"></a>
<a name="line399"></a>
<a name="line400"></a>/** @override */
<a name="line401"></a>goog.dom.TextRange.prototype.isRangeInDocument = function() {
<a name="line402"></a>  // Ensure any cached nodes are in the document.  IE also allows ranges to
<a name="line403"></a>  // become detached, so we check if the range is still in the document as
<a name="line404"></a>  // well for IE.
<a name="line405"></a>  return (!this.startNode_ ||
<a name="line406"></a>          goog.dom.TextRange.isAttachedNode(this.startNode_)) &amp;&amp;
<a name="line407"></a>         (!this.endNode_ ||
<a name="line408"></a>          goog.dom.TextRange.isAttachedNode(this.endNode_)) &amp;&amp;
<a name="line409"></a>         (!(goog.userAgent.IE &amp;&amp; !goog.userAgent.isDocumentModeOrHigher(9)) ||
<a name="line410"></a>          this.getBrowserRangeWrapper_().isRangeInDocument());
<a name="line411"></a>};
<a name="line412"></a>
<a name="line413"></a>
<a name="line414"></a>/** @override */
<a name="line415"></a>goog.dom.TextRange.prototype.isCollapsed = function() {
<a name="line416"></a>  return this.getBrowserRangeWrapper_().isCollapsed();
<a name="line417"></a>};
<a name="line418"></a>
<a name="line419"></a>
<a name="line420"></a>/** @override */
<a name="line421"></a>goog.dom.TextRange.prototype.getText = function() {
<a name="line422"></a>  return this.getBrowserRangeWrapper_().getText();
<a name="line423"></a>};
<a name="line424"></a>
<a name="line425"></a>
<a name="line426"></a>/** @override */
<a name="line427"></a>goog.dom.TextRange.prototype.getHtmlFragment = function() {
<a name="line428"></a>  // TODO(robbyw): Generalize the code in browserrange so it is static and
<a name="line429"></a>  // just takes an iterator.  This would mean we don&#39;t always have to create a
<a name="line430"></a>  // browser range.
<a name="line431"></a>  return this.getBrowserRangeWrapper_().getHtmlFragment();
<a name="line432"></a>};
<a name="line433"></a>
<a name="line434"></a>
<a name="line435"></a>/** @override */
<a name="line436"></a>goog.dom.TextRange.prototype.getValidHtml = function() {
<a name="line437"></a>  return this.getBrowserRangeWrapper_().getValidHtml();
<a name="line438"></a>};
<a name="line439"></a>
<a name="line440"></a>
<a name="line441"></a>/** @override */
<a name="line442"></a>goog.dom.TextRange.prototype.getPastableHtml = function() {
<a name="line443"></a>  // TODO(robbyw): Get any attributes the table or tr has.
<a name="line444"></a>
<a name="line445"></a>  var html = this.getValidHtml();
<a name="line446"></a>
<a name="line447"></a>  if (html.match(/^\s*&lt;td\b/i)) {
<a name="line448"></a>    // Match html starting with a TD.
<a name="line449"></a>    html = &#39;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&#39; + html + &#39;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&#39;;
<a name="line450"></a>  } else if (html.match(/^\s*&lt;tr\b/i)) {
<a name="line451"></a>    // Match html starting with a TR.
<a name="line452"></a>    html = &#39;&lt;table&gt;&lt;tbody&gt;&#39; + html + &#39;&lt;/tbody&gt;&lt;/table&gt;&#39;;
<a name="line453"></a>  } else if (html.match(/^\s*&lt;tbody\b/i)) {
<a name="line454"></a>    // Match html starting with a TBODY.
<a name="line455"></a>    html = &#39;&lt;table&gt;&#39; + html + &#39;&lt;/table&gt;&#39;;
<a name="line456"></a>  } else if (html.match(/^\s*&lt;li\b/i)) {
<a name="line457"></a>    // Match html starting with an LI.
<a name="line458"></a>    var container = this.getContainer();
<a name="line459"></a>    var tagType = goog.dom.TagName.UL;
<a name="line460"></a>    while (container) {
<a name="line461"></a>      if (container.tagName == goog.dom.TagName.OL) {
<a name="line462"></a>        tagType = goog.dom.TagName.OL;
<a name="line463"></a>        break;
<a name="line464"></a>      } else if (container.tagName == goog.dom.TagName.UL) {
<a name="line465"></a>        break;
<a name="line466"></a>      }
<a name="line467"></a>      container = container.parentNode;
<a name="line468"></a>    }
<a name="line469"></a>    html = goog.string.buildString(&#39;&lt;&#39;, tagType, &#39;&gt;&#39;, html, &#39;&lt;/&#39;, tagType, &#39;&gt;&#39;);
<a name="line470"></a>  }
<a name="line471"></a>
<a name="line472"></a>  return html;
<a name="line473"></a>};
<a name="line474"></a>
<a name="line475"></a>
<a name="line476"></a>/**
<a name="line477"></a> * Returns a TextRangeIterator over the contents of the range.  Regardless of
<a name="line478"></a> * the direction of the range, the iterator will move in document order.
<a name="line479"></a> * @param {boolean=} opt_keys Unused for this iterator.
<a name="line480"></a> * @return {!goog.dom.TextRangeIterator} An iterator over tags in the range.
<a name="line481"></a> * @override
<a name="line482"></a> */
<a name="line483"></a>goog.dom.TextRange.prototype.__iterator__ = function(opt_keys) {
<a name="line484"></a>  return new goog.dom.TextRangeIterator(this.getStartNode(),
<a name="line485"></a>      this.getStartOffset(), this.getEndNode(), this.getEndOffset());
<a name="line486"></a>};
<a name="line487"></a>
<a name="line488"></a>
<a name="line489"></a>// RANGE ACTIONS
<a name="line490"></a>
<a name="line491"></a>
<a name="line492"></a>/** @override */
<a name="line493"></a>goog.dom.TextRange.prototype.select = function() {
<a name="line494"></a>  this.getBrowserRangeWrapper_().select(this.isReversed_);
<a name="line495"></a>};
<a name="line496"></a>
<a name="line497"></a>
<a name="line498"></a>/** @override */
<a name="line499"></a>goog.dom.TextRange.prototype.removeContents = function() {
<a name="line500"></a>  this.getBrowserRangeWrapper_().removeContents();
<a name="line501"></a>  this.clearCachedValues_();
<a name="line502"></a>};
<a name="line503"></a>
<a name="line504"></a>
<a name="line505"></a>/**
<a name="line506"></a> * Surrounds the text range with the specified element (on Mozilla) or with a
<a name="line507"></a> * clone of the specified element (on IE).  Returns a reference to the
<a name="line508"></a> * surrounding element if the operation was successful; returns null if the
<a name="line509"></a> * operation failed.
<a name="line510"></a> * @param {Element} element The element with which the selection is to be
<a name="line511"></a> *    surrounded.
<a name="line512"></a> * @return {Element} The surrounding element (same as the argument on Mozilla,
<a name="line513"></a> *    but not on IE), or null if unsuccessful.
<a name="line514"></a> */
<a name="line515"></a>goog.dom.TextRange.prototype.surroundContents = function(element) {
<a name="line516"></a>  var output = this.getBrowserRangeWrapper_().surroundContents(element);
<a name="line517"></a>  this.clearCachedValues_();
<a name="line518"></a>  return output;
<a name="line519"></a>};
<a name="line520"></a>
<a name="line521"></a>
<a name="line522"></a>/** @override */
<a name="line523"></a>goog.dom.TextRange.prototype.insertNode = function(node, before) {
<a name="line524"></a>  var output = this.getBrowserRangeWrapper_().insertNode(node, before);
<a name="line525"></a>  this.clearCachedValues_();
<a name="line526"></a>  return output;
<a name="line527"></a>};
<a name="line528"></a>
<a name="line529"></a>
<a name="line530"></a>/** @override */
<a name="line531"></a>goog.dom.TextRange.prototype.surroundWithNodes = function(startNode, endNode) {
<a name="line532"></a>  this.getBrowserRangeWrapper_().surroundWithNodes(startNode, endNode);
<a name="line533"></a>  this.clearCachedValues_();
<a name="line534"></a>};
<a name="line535"></a>
<a name="line536"></a>
<a name="line537"></a>// SAVE/RESTORE
<a name="line538"></a>
<a name="line539"></a>
<a name="line540"></a>/** @override */
<a name="line541"></a>goog.dom.TextRange.prototype.saveUsingDom = function() {
<a name="line542"></a>  return new goog.dom.DomSavedTextRange_(this);
<a name="line543"></a>};
<a name="line544"></a>
<a name="line545"></a>
<a name="line546"></a>// RANGE MODIFICATION
<a name="line547"></a>
<a name="line548"></a>
<a name="line549"></a>/** @override */
<a name="line550"></a>goog.dom.TextRange.prototype.collapse = function(toAnchor) {
<a name="line551"></a>  var toStart = this.isReversed() ? !toAnchor : toAnchor;
<a name="line552"></a>
<a name="line553"></a>  if (this.browserRangeWrapper_) {
<a name="line554"></a>    this.browserRangeWrapper_.collapse(toStart);
<a name="line555"></a>  }
<a name="line556"></a>
<a name="line557"></a>  if (toStart) {
<a name="line558"></a>    this.endNode_ = this.startNode_;
<a name="line559"></a>    this.endOffset_ = this.startOffset_;
<a name="line560"></a>  } else {
<a name="line561"></a>    this.startNode_ = this.endNode_;
<a name="line562"></a>    this.startOffset_ = this.endOffset_;
<a name="line563"></a>  }
<a name="line564"></a>
<a name="line565"></a>  // Collapsed ranges can&#39;t be reversed
<a name="line566"></a>  this.isReversed_ = false;
<a name="line567"></a>};
<a name="line568"></a>
<a name="line569"></a>
<a name="line570"></a>// SAVED RANGE OBJECTS
<a name="line571"></a>
<a name="line572"></a>
<a name="line573"></a>
<a name="line574"></a>/**
<a name="line575"></a> * A SavedRange implementation using DOM endpoints.
<a name="line576"></a> * @param {goog.dom.AbstractRange} range The range to save.
<a name="line577"></a> * @constructor
<a name="line578"></a> * @extends {goog.dom.SavedRange}
<a name="line579"></a> * @private
<a name="line580"></a> */
<a name="line581"></a>goog.dom.DomSavedTextRange_ = function(range) {
<a name="line582"></a>  /**
<a name="line583"></a>   * The anchor node.
<a name="line584"></a>   * @type {Node}
<a name="line585"></a>   * @private
<a name="line586"></a>   */
<a name="line587"></a>  this.anchorNode_ = range.getAnchorNode();
<a name="line588"></a>
<a name="line589"></a>  /**
<a name="line590"></a>   * The anchor node offset.
<a name="line591"></a>   * @type {number}
<a name="line592"></a>   * @private
<a name="line593"></a>   */
<a name="line594"></a>  this.anchorOffset_ = range.getAnchorOffset();
<a name="line595"></a>
<a name="line596"></a>  /**
<a name="line597"></a>   * The focus node.
<a name="line598"></a>   * @type {Node}
<a name="line599"></a>   * @private
<a name="line600"></a>   */
<a name="line601"></a>  this.focusNode_ = range.getFocusNode();
<a name="line602"></a>
<a name="line603"></a>  /**
<a name="line604"></a>   * The focus node offset.
<a name="line605"></a>   * @type {number}
<a name="line606"></a>   * @private
<a name="line607"></a>   */
<a name="line608"></a>  this.focusOffset_ = range.getFocusOffset();
<a name="line609"></a>};
<a name="line610"></a>goog.inherits(goog.dom.DomSavedTextRange_, goog.dom.SavedRange);
<a name="line611"></a>
<a name="line612"></a>
<a name="line613"></a>/**
<a name="line614"></a> * @return {goog.dom.AbstractRange} The restored range.
<a name="line615"></a> * @override
<a name="line616"></a> */
<a name="line617"></a>goog.dom.DomSavedTextRange_.prototype.restoreInternal = function() {
<a name="line618"></a>  return goog.dom.Range.createFromNodes(this.anchorNode_, this.anchorOffset_,
<a name="line619"></a>      this.focusNode_, this.focusOffset_);
<a name="line620"></a>};
<a name="line621"></a>
<a name="line622"></a>
<a name="line623"></a>/** @override */
<a name="line624"></a>goog.dom.DomSavedTextRange_.prototype.disposeInternal = function() {
<a name="line625"></a>  goog.dom.DomSavedTextRange_.superClass_.disposeInternal.call(this);
<a name="line626"></a>
<a name="line627"></a>  this.anchorNode_ = null;
<a name="line628"></a>  this.focusNode_ = null;
<a name="line629"></a>};
</pre>


</body>
</html>
